package com.zzw.springboot.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzw.springboot.bean.Supplier;
import com.zzw.springboot.bean.SupplierEvaluation;
import com.zzw.springboot.mapper.SupplierEvaluationMapper;
import com.zzw.springboot.mapper.SupplierMapper;
import com.zzw.springboot.service.SupplierEvaluationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

@Service
public class SupplierEvaluationServiceImpl extends ServiceImpl<SupplierEvaluationMapper, SupplierEvaluation> implements SupplierEvaluationService {
    @Autowired
    private SupplierEvaluationMapper evaluationMapper;

    @Autowired
    private SupplierMapper supplierMapper;

    @Override
    @Transactional
    public void addEvaluation(SupplierEvaluation evaluation) {
        evaluationMapper.insertEvaluation(evaluation);
        // 更新供应商评分
        updateSupplierScores(evaluation.getSupplierId());
    }

    @Override
    @Transactional
    public void updateEvaluation(SupplierEvaluation evaluation) {
        evaluationMapper.updateEvaluation(evaluation);
        // 更新供应商评分
        updateSupplierScores(evaluation.getSupplierId());
    }

    @Override
    @Transactional
    public void deleteEvaluation(Integer evaluationId) {
        // 获取评价记录，以便后续更新供应商评分
        SupplierEvaluation evaluation = evaluationMapper.selectEvaluationById(evaluationId);
        if (evaluation != null) {
            evaluationMapper.deleteEvaluation(evaluationId);
            // 更新供应商评分
            updateSupplierScores(evaluation.getSupplierId());
        }
    }

    @Override
    public SupplierEvaluation getEvaluationById(Integer evaluationId) {
        return evaluationMapper.selectEvaluationById(evaluationId);
    }

    @Override
    public List<SupplierEvaluation> getEvaluationsBySupplier(Integer supplierId) {
        return evaluationMapper.selectEvaluationsBySupplier(supplierId);
    }

    @Override
    public List<SupplierEvaluation> getEvaluationList(SupplierEvaluation evaluation) {
        return evaluationMapper.selectEvaluationList(evaluation);
    }

    @Override
    @Transactional
    public void updateSupplierScores(Integer supplierId) {
        // 获取所有评价记录
        List<SupplierEvaluation> evaluations = evaluationMapper.selectEvaluationsBySupplier(supplierId);
        if (evaluations.isEmpty()) {
            return;
        }

        // 计算平均分
        BigDecimal qualityTotal = BigDecimal.ZERO;
        BigDecimal deliveryTotal = BigDecimal.ZERO;
        BigDecimal priceTotal = BigDecimal.ZERO;
        BigDecimal serviceTotal = BigDecimal.ZERO;

        for (SupplierEvaluation eval : evaluations) {
            qualityTotal = qualityTotal.add(eval.getQualityScore());
            deliveryTotal = deliveryTotal.add(eval.getDeliveryScore());
            priceTotal = priceTotal.add(eval.getPriceScore());
            serviceTotal = serviceTotal.add(eval.getServiceScore());
        }

        int count = evaluations.size();
        BigDecimal qualityAvg = qualityTotal.divide(new BigDecimal(count), 1, RoundingMode.HALF_UP);
        BigDecimal deliveryAvg = deliveryTotal.divide(new BigDecimal(count), 1, RoundingMode.HALF_UP);
        BigDecimal priceAvg = priceTotal.divide(new BigDecimal(count), 1, RoundingMode.HALF_UP);
        BigDecimal serviceAvg = serviceTotal.divide(new BigDecimal(count), 1, RoundingMode.HALF_UP);

        // 计算总分
        BigDecimal totalScore = qualityAvg.add(deliveryAvg).add(priceAvg).add(serviceAvg)
                .divide(new BigDecimal(4), 1, RoundingMode.HALF_UP);

        // 根据总分确定等级
        String level = determineLevelByScore(totalScore);

        // 更新供应商
        Supplier supplier = new Supplier();
        supplier.setSupplierId(supplierId);
        supplier.setQualityScore(qualityAvg);
        supplier.setDeliveryScore(deliveryAvg);
        supplier.setPriceScore(priceAvg);
        supplier.setServiceScore(serviceAvg);
        supplier.setLevel(level);

        supplierMapper.updateSupplier(supplier);
    }

    // 根据分数确定等级
    private String determineLevelByScore(BigDecimal score) {
        if (score.compareTo(new BigDecimal("9.0")) >= 0) {
            return "S"; // 战略供应商
        } else if (score.compareTo(new BigDecimal("8.0")) >= 0) {
            return "A"; // 优质供应商
        } else if (score.compareTo(new BigDecimal("7.0")) >= 0) {
            return "B"; // 良好供应商
        } else if (score.compareTo(new BigDecimal("6.0")) >= 0) {
            return "C"; // 一般供应商
        } else {
            return "D"; // 待观察供应商
        }
    }
}
